package info.wegosoft.android.util;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**  
 * Filename:    TimeUtil.java 
 * Package:     info.wegosoft.android.util 
 * Description: 时间处理工具类
 * Copyright:   Copyright (c) wegosoft.info 2011  
 * @author:     李辉 
 * email:       fzlihui@gmail.com
 * @version:    1.0  
 * Create at:   2011-5-11 下午11:01:26  
 */
public class TimeUtil {

	private static SimpleDateFormat sdf = new SimpleDateFormat("EEE MMM dd hh:mm:ss Z yyyy",Locale.ENGLISH);
	private static SimpleDateFormat sdf1 = new SimpleDateFormat("MM-dd HH:mm");
		
	/**
	 * 将Date转换为字符串，格式为MM-dd HH:mm
	 * @param date
	 * @return
	 */
	public static String formatDateToString(Date date){
		return sdf1.format(date);
	}
	
	/**
	 * 将Date转换为腾讯时间戳字符串，格式为MM-dd HH:mm
	 * @param date
	 * @return
	 */
	public static String formatDateToTencentString(Date date){
		String time = Long.toString(date.getTime());
		
		time = time.substring(0, time.length() - 3);
		
		return time;
	}
	
	/**
	 * 将新浪字符串时间格式化为Date对象
	 * @param time 字符串时间，类似Sun Apr 24 00:00:00 +0800 2011
	 * @return
	 */
	public static Date formatSinaStringToDate(String time) {
		return sdf.parse(time,new ParsePosition(0));
	}
	
	/**
	 * 将腾讯字符串时间格式化为Date对象
	 * @param time 字符串时间，类似1305675978 （缺少最后三位）
	 * @return
	 */
	public static Date formatTencentStringToDate(String time) {
		
		return new Date(Long.parseLong(time + "000"));
		
	}
	
	/**
	 * 计算两个日期相差时间，相差一分钟之内，返回相差秒数；
	 * 如果相差一小时之内，返回相差分钟；相差几天，返回相差时间
	 * @param one
	 * @param two
	 * @return 返回相差时间
	 */
	public static String timeBetween(Date one,Date two){
		long oneMM = one.getTime();
        long twoMM = two.getTime();
        long diff; //两个时间相差的毫秒数
        if(oneMM < twoMM) {
            diff = twoMM - oneMM;
        } else {
            diff = oneMM - twoMM;
        }
        
        if(diff < 60000){
        	//如果相差一分钟之内
        	return Integer.toString((int) diff/1000) + "秒前";
        }else{
        	if(diff < 3600000){
        		//如果相差一小时之内
            	return Integer.toString((int) diff/60000) + "分钟前";
        	}else{
        		if(diff < 216000000){
        			//如果相差一天之内
                	return Integer.toString((int) diff/3600000) + "小时前";
        		}else {
        			return Integer.toString((int) diff/86400000) + "天前";
				}
        	}
        }
	}
	
}
